//
//  YCWelcomeVC.swift
//  weiBoSwift
//
//  Created by 刘胤辰 on 16/7/24.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit
import SDWebImage

class YCWelcomeVC: UIViewController {
    
    /*
    思路：1.首先有个头像，要获取用户的信息：此处需要userAccount
         2.头像下部附着一个label(头像停止的时候显示出来，并且带有一个动画)
         3.动画结束加载mainView
    */
    
    // MARK: -- 懒加载控件
    private lazy var headImg:UIImageView = {
        
        let head = UIImageView()
//        guard let userAccount = SHUserAccountViewModel.sharedTools.userAccount else{
//            
//            return
//        }
        //通过自定义sdwebimage来进行图片的加载
        head.yc_webImage(SHUserAccountViewModel.sharedTools.userAccount?.avatar_large, imageName: "avatar_default_big")
        head.sizeToFit()
        
        head.layer.cornerRadius=45
        head.layer.masksToBounds=true
        
        head.layer.borderColor=UIColor.redColor().CGColor
        head.layer.borderWidth=2
        
        return head
    }()
    
    private lazy var gibView : UIImageView = UIImageView(imageName: "ad_background")

    private lazy var nameLabel : UILabel=UILabel(text: "欢迎光临", textColor: UIColor.blackColor(), font: 16)
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(true)
        
        setAnimination()
    }
    
    private func setUI(){
        
        view.backgroundColor=UIColor.whiteColor()
        nameLabel.alpha=0
        //添加
        view.addSubview(gibView)
        view.addSubview(headImg)
        view.addSubview(nameLabel)
        
        //设置约束
        headImg.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view.snp_bottom).offset(30)
        }
        gibView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImg.snp_bottom).offset(20)
            make.centerX.equalTo(view)
        }
    }
    
    private func setAnimination(){
        
        headImg.snp_updateConstraints() { (make) -> Void in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view.snp_bottom).offset(-500)
        }
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }){ (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.nameLabel.alpha=1.0
                    }, completion: { (_) -> Void in
         
                    //更改根控制器(跳到主界面)
                    NSNotificationCenter.defaultCenter().postNotificationName("changeVC", object: "welcomeVC")
                })
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
